package com.ttuproject.ttu.service.serviceImpl;

import com.ttuproject.ttu.dao.InstallationAccount;
import com.ttuproject.ttu.dao.User;
import com.ttuproject.ttu.exception.Advice;
import com.ttuproject.ttu.exception.TTUException;
import com.ttuproject.ttu.mapper.TTUMapper.UpdateStationMapper;
import com.ttuproject.ttu.mapper.TTUMapper.UserMapper;
import com.ttuproject.ttu.service.UpdateStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UpdateStationServiceImpl
 * @Description TODO
 * @Author cw
 * @Date 2021/12/9 11:34
 */
@Service
public class UpdateStationServiceImpl implements UpdateStationService {

    @Autowired
    UpdateStationMapper updateStationMapper;
    @Autowired
    UserMapper userMapper;


    @Override
    public InstallationAccount queryStation(String linkage1, String linkage2, String linkage3) {
        InstallationAccount installationAccount = updateStationMapper.queryStation(linkage1, linkage2, linkage3);
        if (installationAccount == null) {
            throw new TTUException(Advice.QUERY_STATION_ERROR);
        } else {
            return installationAccount;
        }
    }

    @Override
    public boolean updateStationList(InstallationAccount installationAccount,String userName,String passWord) {
        //权限判断
        User user = userMapper.selectUser(userName, passWord);
        if (user == null ||  user.getPower() == 2) {
            //不是管理员，没有权限使用更新台账功能
            throw new TTUException(Advice.INSUFFICIENT_PERMISSIONS);
        }
        System.out.println("installationAccount = " + installationAccount);
        boolean updateStationType = updateStationMapper.updateStationList(installationAccount);
        if (updateStationType) {
            return true;
        } else {
            throw new TTUException(Advice.UPDATE_STATIONLIST_ERROR);
        }
    }

    @Override
    public boolean insertStationList(InstallationAccount installationAccount,String userName,String passWord) {
        //权限判断
        User user = userMapper.selectUser(userName, passWord);
        if (user == null || user.getPower() == 2) {
            //不是管理员，没有权限使用更新台账功能
            throw new TTUException(Advice.INSUFFICIENT_PERMISSIONS);
        }
        boolean insertStationType = updateStationMapper.insertStationList(installationAccount);
        if (insertStationType) {
            return true;
        } else {
            throw new TTUException(Advice.INSERT_STATIONLIST_ERROR);
        }

    }
}
